﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyDoanVien.Controller;
using QuanLyDoanVien.Bussiness;
using QuanLyDoanVien.General;

namespace QuanLyDoanVien
{
    public partial class frmQLKL : DevComponents.DotNetBar.Office2007Form
    {
        KyLuatCtrl m_KyLuatCtrl = new KyLuatCtrl();
        List<KyLuatInfo> dskl = new List<KyLuatInfo>();
        QuyDinh m_QuyDinh = new QuyDinh();
        public frmQLKL()
        {
            InitializeComponent();
            DataService.OpenConnection();
        }
        private KyLuatInfo New_KyLuat()
        {
            String mkl = m_KyLuatCtrl.getAutoID();
            if (this.txt_KL_GhiChu.Text == "")
            {
                this.txt_KL_GhiChu.Text = " ";
            }
            if (txt_KL_Ten.Text == "")
            {
                MessageBoxEx.Show("Chưa Nhập Tên Kỷ Luật: ", "ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
            KyLuatInfo kl = new KyLuatInfo(mkl,txt_KL_Ten.Text,txt_KL_GhiChu.Text);
            return kl;
        }
        private void Refesh_KyLuat()
        {
            this.txt_KL_Ma.Text = "";
            this.txt_KL_Ten.Text = "";
            this.txt_KL_GhiChu.Text = "";
        }
        private void Load_DS_KyLuat()
        {
            dataGridView_KL.Rows.Clear();
            dskl = m_KyLuatCtrl.layDSKyLuat();
            foreach (KyLuatInfo k in dskl)
            {
                dataGridView_KL.Rows.Add(k.MaKyLuat,k.TenKyLuat,k.GhiChu);
            }
            
        }
        private void btn_KL_Them_Click(object sender, EventArgs e)
        {
            KyLuatInfo kl = New_KyLuat();
            m_KyLuatCtrl.ThemKyLuat(kl);
            Load_DS_KyLuat();
            Refesh_KyLuat();

        }

        private void frmQLKL_Load(object sender, EventArgs e)
        {
            Load_DS_KyLuat();
        }

        private void btn_KL_Xoa_Click(object sender, EventArgs e)
        {
            m_KyLuatCtrl.XoaKyLuat(txt_KL_Ma.Text);
            Load_DS_KyLuat();
            //this.Refesh_KyLuat();
        }

        private void dataGridView_KL_SelectionChanged(object sender, EventArgs e)
        {
            int index;
            if (dataGridView_KL.SelectedRows.Count <= 0 || dataGridView_KL.SelectedRows[0].Index == dataGridView_KL.RowCount - 1)
            {
                return;
            }
            else
            {
                index = dataGridView_KL.SelectedRows[0].Index;
            }
            DataGridViewRow dr = dataGridView_KL.Rows[index];
            this.txt_KL_Ma.Text = dr.Cells["MaKyLuat"].Value.ToString();
            this.txt_KL_Ten.Text = dr.Cells["TenKyLuat"].Value.ToString();
            this.txt_KL_GhiChu.Text = dr.Cells["GhiChu"].Value.ToString();

        }

        private void btn_KL_Luu_Click(object sender, EventArgs e)
        {
            KyLuatInfo kl = new KyLuatInfo(txt_KL_Ma.Text, txt_KL_Ten.Text, txt_KL_GhiChu.Text);
            m_KyLuatCtrl.SuaKyLuat(kl);
            Load_DS_KyLuat();
        }

        private void btn_KL_Sua_Click(object sender, EventArgs e)
        {
            Load_DS_KyLuat();
            this.Refesh_KyLuat();
            this.txt_KL_Ma.Text = m_KyLuatCtrl.getAutoID();

        }
    }
}